<template>
    <div class="home">
        <img alt="Vue logo" src="../assets/logo.png">
        <div>
            <button @click="serialList()">获取列表</button>
        </div>
        <div>
            <button @click="closeCom()">关闭串口</button>
        </div>
        <div>
            <button @click="sendCom()">发送消息</button>
        </div>
        <div>
            <button @click="openPort()">打开串口com7</button>
        </div>


    </div>
</template>

<script>
    // @ is an alias to /src
    import {ipcRenderer} from 'electron'
    //串口
    // const SerialPort = require('serialport')


    export default {
        name: "SerialportTest",
        data() {
            return {
                list: [],
                SerialSetting: {
                    baudRate: 4800, //波特率
                    dataBits: 8, //数据位
                    parity: "none", //奇偶校验
                    stopBits: 1, //停止位
                    flowControl: false,
                    autoOpen: false
                }
            }
        },
        created() {
            window.ipcRenderer = ipcRenderer





        },
        methods: {
            //获取列表
            serialList() {
                //串口列表
                ipcRenderer.removeAllListeners('list')
                ipcRenderer.on('list', (event, error, ports) => {
                    if (error) {
                        console.log("串口列表获取失败 ： ",error)
                    } else {
                        console.log("串口列表获取成功")
                        ports.forEach(function (port) {
                            console.log(port)
                        })
                    }

                })
                ipcRenderer.send('serialList')
            },
            //获取列表
            sendCom() {
                //发送状态更新
                ipcRenderer.removeAllListeners('write')
                ipcRenderer.on('write', (event, error) => {
                    if(error){
                        console.log("发送失败 ",error)
                    }else {
                        console.log("发送成功 ")
                    }
                })
                ipcRenderer.send('sendSerial', '发送消息')
            },
            //关闭串口
            closeCom() {
                ipcRenderer.removeAllListeners('close')
                ipcRenderer.on('close', (event, error) => {
                    if (error) {
                        console.log("串口关闭失败 ： ",error)
                    } else {
                        console.log("串口关闭成功")
                    }

                })
                ipcRenderer.send('closeSerial')
            },
            //打开串口
            openPort() {
                console.log("监听com7..")
                ipcRenderer.removeAllListeners('open')
                //打开状态更新
                ipcRenderer.on('open', (event, error) => {
                    if(error){
                        console.log("串口打开失败 ",error)
                    }else {
                        console.log("串口打开成功 ")
                        //监听串口数据
                        ipcRenderer.removeAllListeners('marh-data')
                        ipcRenderer.on('marh-data', (event, arg) => {
                            console.log("监听arg：", arg)
                        })
                    }
                })
                ipcRenderer.send('openPort', {
                    path: 'COM7',
                    setting: this.SerialSetting
                })
            },
        }
    }
</script>
